# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def buildTree(self, inorder: list, postorder: list) -> TreeNode:
        if not inorder:
            return None
        mid_root, root_index = TreeNode(postorder[-1]), inorder.index(postorder[-1])
        left_inorder, right_inorder = inorder[:root_index], inorder[root_index+1:]
        left_postorder, right_postorder = postorder[:len(left_inorder)], postorder[len(left_inorder):-1]
        mid_root.left = self.buildTree(left_inorder, left_postorder)
        mid_root.right = self.buildTree(right_inorder, right_postorder)
        return mid_root


a = Solution()
b = a.buildTree([9,3,15,20,7], [9,15,7,20,3])